package com.hd.manager.util;

import com.alibaba.fastjson.JSONObject;
import com.hd.appcommon.constant.URLConstant;
import com.hd.appcommon.parse.DDFormParser;
import com.hd.appcommon.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.conn.ConnectTimeoutException;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SensitiveMessageUtil {
    private static String xgSaasToken;
    private static Long tokenTime = System.currentTimeMillis() / 1000;

    public static String getXGSaasToken() {
        // 判断是否为null或者过期
        long now = System.currentTimeMillis() / 1000;
        // token 为null的时候获取, 或者离过期只有一分钟的时候获取
        if (StringUtils.isEmpty(xgSaasToken) || now - tokenTime > 7140) {
            JSONObject result = new JSONObject();
            try {
                String responsString = HttpClientUtils.get(URLConstant.URL_XGSAAS_TOKEN_GET);
                log.info("url: URLConstant.URL_XGSAASTOKEN_GET get token");
                result = JSONObject.parseObject(responsString);
            } catch (IOException e) {
                e.printStackTrace();
            }
            JSONObject data = result.getJSONObject("data");
            xgSaasToken = data.getString("access_token");
            // 获得毫秒除以1000
            tokenTime = System.currentTimeMillis() / 1000;
        }

        return xgSaasToken;
    }

    public static JSONObject getXGSaasSensitive(String urlString, String token, Integer page, Integer size) {

        StringBuilder url = new StringBuilder(urlString);
        url.append("?").append("access_token=").append(token)
                .append("&page=").append(page).append("&size=").append(size);

        JSONObject result = new JSONObject();
        try {
            String responsString = HttpClientUtils.get(url.toString());
            log.info("url: URLConstant.URL_XGSAAS_TSENSITIVE_GET get XGSaas Sensitive information , page : {}", page);
            result = JSONObject.parseObject(responsString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static JSONObject getStudentTransactionInformation(String urlString, String token,String YDXN, Integer page, Integer size) {

        StringBuilder url = new StringBuilder(urlString);
        url.append("?").append("access_token=").append(token).append("&YDXN=").append(YDXN)
                .append("&page=").append(page).append("&size=").append(size);

        JSONObject result = new JSONObject();
        try {
            String responsString = HttpClientUtils.get(url.toString());
            log.info("url: URLConstant.URL_XGSAAS_STUDENT_TRANSACTION_INFORMATION_GET get XGSaas Unnormal information , page : {}", page);
            result = JSONObject.parseObject(responsString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static JSONObject getXGSaasStudentCourseMsg(String urlString, String token, String XN,String term, Integer page, Integer size) {
        return getXGSaasStudentCourseMsgRecursion(urlString, token, XN, term, page, size,0);
    }

    public static JSONObject getXGSaasStudentCourseMsgRecursion(String urlString, String token, String XN,String term, Integer page, Integer size, Integer deep){

        StringBuilder url = new StringBuilder(urlString);
        url.append("?").append("access_token=").append(token);
        Map<String,String> map = new HashMap<>();
        map.put("XN", XN);
        map.put("XQM", term);
        map.put("page",page.toString());
        map.put("size",size.toString());
        JSONObject result = new JSONObject();
        try {
            String responsString = HttpClientUtils.post(url.toString(), map);
            log.info("url: URLConstant.URL_XGSAAS_TSENSITIVE_GET get XGSaas Sensitive information , page : {}", page);
            result = JSONObject.parseObject(responsString);
        } catch ( SocketTimeoutException | ConnectTimeoutException e){
            log.info("开始超时重传");
            //  超时重传10次
            if(deep<10)
            return getXGSaasStudentCourseMsgRecursion(urlString, token, XN, term, page, size, deep+1);
            else{
                log.error(e.toString());
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
